﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using NPOI.HSSF.UserModel;
using System.IO;

namespace Taxes.Caculate
{
    public partial class BuildingTaxes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [DirectMethod]
        public void DeleteFile(string fileName, string fileClientName)
        {
            try
            {
                System.IO.File.Delete(fileName);
            }
            catch (Exception ex)
            {
            }
        }

        [DirectMethod]
        public void Up(string att)
        {
            var path = System.Configuration.ConfigurationManager.AppSettings["UploadPath"].ToString();
            var fileName = Server.MapPath(path + "/" + att);
            List<Model.tax_info> infos = new List<Model.tax_info>();
            bool isComplete = false;
            using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                var workBook = new HSSFWorkbook(stream);
                var enumerator = workBook.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    var sheet = (HSSFSheet)enumerator.Current;
                    if (sheet == null) continue;
                    var rowEnumerator = sheet.GetEnumerator();
                   
                    while (rowEnumerator.MoveNext())
                    {
                        var row = (HSSFRow)rowEnumerator.Current;
                        if (row == null)
                            break;
                        if (row.RowNum < 2)
                            continue;
                        var info = new Model.tax_info();
                        var cellEnumerator = row.GetEnumerator();
                        isComplete = false;
                        while (cellEnumerator.MoveNext())
                        {
                            var cell = cellEnumerator.Current as HSSFCell;
                            if (cell == null) continue;
                            if (cell.ColumnIndex == 0)
                            {
                                if (cell.StringCellValue == "" || cell == null)
                                    break;
                            }
                            switch (cell.ColumnIndex)
                            {
                                case 1:
                                    //计算机代码
                                    info.compute_code = cell.StringCellValue;
                                    break;
                                case 3:
                                    info.tax_money = Convert.ToDecimal(cell.StringCellValue);
                                    isComplete = true;
                                    break;
                            }
                        }
                        info.tax_type = 1;
                        info.tax_ny = (DateTime.Now - new TimeSpan(32, 0, 0, 0, 0)).Year.ToString()+"00";
                        if (isComplete)
                            infos.Add(info);
                    }
                }
            }
            var bll = new BLL.tax_info();
            foreach (var i in infos)
            {

                if (bll.up_setTaxInfo(i.compute_code, i.tax_ny, i.tax_type, i.tax_money))
                {
                    String s = "";
                }
            }

        }
    }
}